package com.hckjsec.law.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hckjsec.common.general.MapLongView;
import com.hckjsec.common.general.MapStringView;
import com.hckjsec.common.utils.*;
import com.hckjsec.law.domain.*;
import com.hckjsec.law.service.*;
import com.hckjsec.system.service.ISysPostService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公共法律服务咨询Controller
 * 
 * @author cyt
 * @date 2021-11-08
 */
@RestController
@RequestMapping("/system/lawconsulling")
public class WebLawConsullingController extends BaseController
{
    @Autowired
    private IWebLawConsullingService webLawConsullingService;

    @Autowired
    private ILawCaseTypeService lawCaseTypeService;

    @Autowired
    private IWebLawCompanyService webLawCompanyService;

    @Autowired
    private IWebLawConsullingDetailService webLawConsullingDetailService;

    @Autowired
    private ILawWechatUserService lawWechatUserService;

    @Autowired
    private ISysPostService postService;

    /**
     * 查询公共法律服务咨询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebLawConsulling webLawConsulling)
    {
        startPage();
        if(!SecurityUtils.isAdminPost(SecurityUtils.getUserId(),postService.selectPostListByUserId(SecurityUtils.getUserId()))){
            webLawConsulling.setDeptId(SecurityUtils.getDeptId());
            webLawConsulling.getParams().put("personal", SecurityUtils.getUserId());
        }
        List<WebLawConsulling> list = webLawConsullingService.selectWebLawConsullingList(webLawConsulling);
        return getDataTable(list);
    }

    /**
     * 导出公共法律服务咨询列表
     */
    @Log(title = "公共法律服务咨询", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebLawConsulling webLawConsulling)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webLawConsulling.setDeptId(SecurityUtils.getDeptId());
            webLawConsulling.getParams().put("personal", SecurityUtils.getUserId());
        }
        List<WebLawConsulling> list = webLawConsullingService.selectWebLawConsullingList(webLawConsulling);
        ExcelUtil<WebLawConsulling> util = new ExcelUtil<WebLawConsulling>(WebLawConsulling.class);
        return util.exportExcel(list, "consulling");
    }

    /**
     * 获取公共法律服务咨询详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webLawConsullingService.selectWebLawConsullingById(id));
    }

    /**
     * 新增公共法律服务咨询
     */
    @Log(title = "公共法律服务咨询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebLawConsulling webLawConsulling)
    {
        return toAjax(webLawConsullingService.insertWebLawConsulling(webLawConsulling));
    }

    /**
     * 修改公共法律服务咨询
     */
    @Log(title = "公共法律服务咨询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebLawConsulling webLawConsulling)
    {
        return toAjax(webLawConsullingService.updateWebLawConsulling(webLawConsulling));
    }

    /**
     * 删除公共法律服务咨询
     */
    @Log(title = "公共法律服务咨询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webLawConsullingService.deleteWebLawConsullingByIds(ids));
    }

    /**
     * 获取机构获取问题类型
     */
    @GetMapping("/getLawCaseTypeByDeptList")
    public AjaxResult getLawCaseTypeByDeptList()
    {
       WebLawCompany webLawCompany = new WebLawCompany();
       webLawCompany.setDeptId(SecurityUtils.getDeptId());
       List<WebLawCompany> webLawCompanyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
       String companyType = "";
       if (!StringUtils.isEmpty(webLawCompanyList) ){
           companyType = webLawCompanyList.get(0).getCompanyType();
       }
       LawCaseType lawCaseType = new LawCaseType();
       lawCaseType.setState("true");
       if (!"".equals(companyType)){
           lawCaseType.getParams().put("number", companyType);
       }
       List<LawCaseType> lawCaseTypeList = lawCaseTypeService.selectLawCaseTypeList(lawCaseType);
       List<MapStringView> htList = new ArrayList<>();
       lawCaseTypeList.forEach(kv->{
            htList.add(new MapStringView(kv.getTypeName(),kv.getTypeNumber()));
       });
       return AjaxResult.success(htList);
    }

    /**
     * 获取机构map
     */
    @GetMapping("/getCompanyListHt")
    public AjaxResult getCompanyListHt()
    {
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(null);
        List<MapLongView> htList = new ArrayList<>();
        companyList.forEach(kv->{
            htList.add(new MapLongView(kv.getCompanyName(),kv.getId()));
        });
        return AjaxResult.success(htList);
    }

    /**
     * 获取公共法律服务咨询回复信息
     */
    @GetMapping(value = "/getReply/{id}")
    public AjaxResult getReply(@PathVariable("id") Long id)
    {
        WebLawConsullingDetail webLawConsullingDetail = new WebLawConsullingDetail();
        webLawConsullingDetail.setConsullingId(id);
        List<WebLawConsullingDetail> list = webLawConsullingDetailService.selectWebLawConsullingDetailList(webLawConsullingDetail);
        list.forEach(kv->{
            String openid = StringUtils.isHcNull(kv.getOpenid(), "");
            if (!"".equals(openid)){
                LawWechatUser lawWechatUser = lawWechatUserService.selectLawWechatUserById(openid);
                kv.setOpenid(lawWechatUser.getAvatarurl());
            }
        });
        return AjaxResult.success(list);
    }


    /**
     * 公共法律服务咨询回复信息
     */
    @PostMapping(value = "/setReply")
    public AjaxResult setReply(@RequestBody WebLawConsullingDetail webLawConsullingDetail)
    {
        List<String> phonelist = new ArrayList<String>();
        webLawConsullingDetail.setUserId(SecurityUtils.getUserId());
        webLawConsullingDetail.setType("1");
        webLawConsullingDetail.setUserName(SecurityUtils.getNickName());
        webLawConsullingDetail.setOperationTime(DateUtils.getNowDate());
        int rows = webLawConsullingDetailService.insertWebLawConsullingDetail(webLawConsullingDetail);
        if (rows>0){
            WebLawConsulling webLawConsulling = webLawConsullingService.selectWebLawConsullingById(webLawConsullingDetail.getConsullingId());
            webLawConsulling.setState("1");
            webLawConsulling.setReplyUser(SecurityUtils.getUserId());
            webLawConsulling.setReplyTime(DateUtils.getNowDate());
            webLawConsullingService.updateWebLawConsulling(webLawConsulling);
            phonelist.add("+86"+webLawConsulling.getPhone());
            TxSmsUtil.smsData(ComUtil.LAW_SECRETID,ComUtil.LAW_SECRETKEY,ComUtil.LAW_SMSSDKAPPID,ComUtil.LAW_SIGN,phonelist,"14","","","");
        }
        return toAjax(rows);
    }


    /**
     * 公共法律服务咨询回复信息-已结束
     */
    @PostMapping(value = "/setReplyEnd")
    public AjaxResult setReplyEnd(@RequestBody WebLawConsulling webLawConsulling)
    {
        WebLawConsulling findwebLawConsulling = webLawConsullingService.selectWebLawConsullingById(webLawConsulling.getId());
        findwebLawConsulling.setState("3");
        findwebLawConsulling.setCloseUser(SecurityUtils.getUserId());
        findwebLawConsulling.setCloseTime(DateUtils.getNowDate());
        return toAjax(webLawConsullingService.updateWebLawConsulling(findwebLawConsulling));
    }
}
